package views.searchViews;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;


import controllers.VerwijderPassagierController;
import controllers.searchControllers.ZoekPassagierController;
import controllers.updateControllers.UpdatePassagierController;
import core.logic.services.VerwijderPassagierService;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.searchModels.ZoekPassagierModel;
import core.models.domainModel.Passagier;

import java.awt.Component;

public class ZoekPassagierView extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtVoornaam;
	private JTextField txtAchternaam;
	private ZoekPassagierModel _model;
	private JTextField txtPassagierID;
	final JComboBox<String> cmbVluchtnummer;
	private JList gevondenPassagiersJList = new JList();

	/**
	 * Create the frame.
	 */
	public ZoekPassagierView(ZoekPassagierModel model) {

		// panel aanmaken om je invoervelden en labels in te zetten.
		JPanel panel_1 = new JPanel();
		JLabel lblPassagierid = new JLabel("PassagierID:");
		JLabel lblNewLabel = new JLabel(
				"Zoek passagiers aan de hand van de volgende gegevens:");
		JLabel lblVoornaam = new JLabel("Voornaam:");
		JLabel lblFamilienaam = new JLabel("Familienaam:");
		JLabel lblVluchtnummer = new JLabel("Vluchtnummer:");
		_model = model;
		cmbVluchtnummer = new JComboBox<String>();

		// invoervelden aanmaken
		txtVoornaam = new JTextField();
		txtVoornaam.setColumns(10);
		txtAchternaam = new JTextField();
		txtAchternaam.setColumns(10);
		txtPassagierID = new JTextField();
		txtPassagierID.setColumns(10);

		// standaardknop toevoegen
		// txtVoornaam.setDefaultButton(btnZoek);

		// eigenschappen scherm aanpassen
		setTitle("Zoek Passagiers");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 619, 462);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		// Zoek knop
		JButton btnZoek = new JButton("Zoek");

		btnZoek.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {

					// tekstvakken in model ingeven of leeg maken
					if (!(txtPassagierID.getText().equals(""))) {

						if (ValidatieService.isInteger(txtPassagierID, "PassagierID")) {
							_model.setPassagierId(Integer.parseInt(txtPassagierID.getText()));
							fillModel();
						}
						else {
							resetten();
						}
					} else {
						_model.setPassagierId(null);
						fillModel();
					}


					if (_model.getSchermModus() == "DELETE") {
						VerwijderPassagierController.handleView(_model);
					} else {
						if (_model.getSchermModus() == "Update") {
							UpdatePassagierController.handleViewSearch(_model);
						} else {
							ZoekPassagierController.handleView(_model);
						}
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				}

			}
		});

		JButton btnPassagierVerwijderen = new JButton("Passagier Verwijderen");
		btnPassagierVerwijderen.setVisible(false);
		if (_model.getSchermModus() == "DELETE") {
			btnPassagierVerwijderen.setVisible(true);
		}
		btnPassagierVerwijderen.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (gevondenPassagiersJList.getSelectedIndex() != -1) {
					VerwijderPassagierService
					.verwijderPassagier((Passagier) gevondenPassagiersJList
							.getSelectedValue());
				}
			}
		});

		// combobox opvullen met eerst een leeg element
		cmbVluchtnummer.addItem("");
		for (String dit : _model.getMogelijkeVluchten2()) {
			cmbVluchtnummer.addItem(dit);
		}

		if (_model.getPassagierId() != null && _model.getPassagierId() > 0) {
			txtPassagierID.setText(_model.getPassagierId().toString());
		} else {
			txtPassagierID.setText(null);
		}
		if (_model.getVoornaam() != null && _model.getVoornaam().length() > 0) {
			txtVoornaam.setText(_model.getVoornaam());
		} else {
			txtVoornaam.setText(null);
		}
		if (_model.getFamilienaam() != null
				&& _model.getFamilienaam().length() > 0) {
			txtAchternaam.setText(_model.getFamilienaam());
		} else {
			txtAchternaam.setText(null);
		}
		if (_model.getVlucht() != null && _model.getVlucht() != "") {
			cmbVluchtnummer.setSelectedItem(_model.getVlucht());
		} else {
			cmbVluchtnummer.setSelectedIndex(0);
		}

		// Knop om alle velden terug leeg te maken
		JButton btnWisGegevens = new JButton("Wis gegevens");
		btnWisGegevens.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnWisGegevens.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				resetten();

			}
		});

		JScrollPane listScroller_1 = new JScrollPane();

		DefaultListModel<Passagier> lstPassagierString = new DefaultListModel<Passagier>();
		if (_model.getPassagierslijst() != null
				&& _model.getPassagierslijst().size() > 0) {
			for (Passagier item : _model.getPassagierslijst()) {

				lstPassagierString.addElement(item);
			}
		}
		_model.setPassagierslijst(null);

		JList list_1 = new JList();

		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
		.setHorizontalGroup(gl_panel_1
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						Alignment.LEADING,
						gl_panel_1
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								gl_panel_1
								.createParallelGroup(
										Alignment.LEADING)
										.addComponent(
												lblPassagierid)
												.addComponent(
														lblVoornaam)
														.addComponent(
																lblFamilienaam)
																.addComponent(
																		lblVluchtnummer))
																		.addGap(18)
																		.addGroup(
																				gl_panel_1
																				.createParallelGroup(
																						Alignment.TRAILING)
																						.addComponent(
																								cmbVluchtnummer,
																								Alignment.LEADING,
																								0, 201,
																								Short.MAX_VALUE)
																								.addComponent(
																										txtAchternaam,
																										Alignment.LEADING,
																										GroupLayout.DEFAULT_SIZE,
																										202,
																										Short.MAX_VALUE)
																										.addComponent(
																												txtVoornaam,
																												Alignment.LEADING,
																												GroupLayout.DEFAULT_SIZE,
																												202,
																												Short.MAX_VALUE)
																												.addComponent(
																														txtPassagierID,
																														Alignment.LEADING,
																														GroupLayout.DEFAULT_SIZE,
																														202,
																														Short.MAX_VALUE))
																														.addGap(107)));
		gl_panel_1
		.setVerticalGroup(gl_panel_1
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel_1
						.createSequentialGroup()
						.addGap(21)
						.addGroup(
								gl_panel_1
								.createParallelGroup(
										Alignment.BASELINE)
										.addComponent(
												lblPassagierid)
												.addComponent(
														txtPassagierID,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(
																ComponentPlacement.RELATED)
																.addGroup(
																		gl_panel_1
																		.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						txtVoornaam,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								lblVoornaam))
																								.addPreferredGap(
																										ComponentPlacement.RELATED)
																										.addGroup(
																												gl_panel_1
																												.createParallelGroup(
																														Alignment.BASELINE)
																														.addComponent(
																																lblFamilienaam)
																																.addComponent(
																																		txtAchternaam,
																																		GroupLayout.PREFERRED_SIZE,
																																		GroupLayout.DEFAULT_SIZE,
																																		GroupLayout.PREFERRED_SIZE))
																																		.addGap(5)
																																		.addGroup(
																																				gl_panel_1
																																				.createParallelGroup(
																																						Alignment.BASELINE)
																																						.addComponent(
																																								lblVluchtnummer)
																																								.addComponent(
																																										cmbVluchtnummer,
																																										GroupLayout.PREFERRED_SIZE,
																																										GroupLayout.DEFAULT_SIZE,
																																										GroupLayout.PREFERRED_SIZE))
																																										.addContainerGap(16, Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		// Passagier updaten
		// button inschakelen indien nodig
		// geselecteerde passsagier meesturen of waarschuwing indien geen
		// selectie

		JButton btnPassaUpdate = new JButton("Passagier Wijzigen");

		if (_model.getSchermModus() == "Update") {
			btnPassaUpdate.setEnabled(true);
		} else {
			btnPassaUpdate.setEnabled(false);
		}

		btnPassaUpdate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				if (gevondenPassagiersJList.getSelectedValue() != null) {
					UpdatePassagierController
					.showUpdateView((Passagier) gevondenPassagiersJList
							.getSelectedValue());
				}

				else {
					JOptionPane.showMessageDialog(null,
							"Gelieve een passagier te selecteren");
				}
			}
		});

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
		.setHorizontalGroup(gl_contentPane
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_contentPane
						.createSequentialGroup()
						.addGroup(
								gl_contentPane
								.createParallelGroup(
										Alignment.LEADING)
										.addComponent(
												lblNewLabel,
												GroupLayout.PREFERRED_SIZE,
												564,
												GroupLayout.PREFERRED_SIZE)
												.addGroup(
														gl_contentPane
														.createSequentialGroup()
														.addContainerGap()
														.addComponent(
																btnZoek,
																GroupLayout.PREFERRED_SIZE,
																117,
																GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																		.addComponent(
																				btnWisGegevens,
																				GroupLayout.PREFERRED_SIZE,
																				128,
																				GroupLayout.PREFERRED_SIZE)
																				.addPreferredGap(
																						ComponentPlacement.RELATED)
																						.addComponent(
																								btnPassagierVerwijderen)
																								.addPreferredGap(
																										ComponentPlacement.RELATED)
																										.addComponent(
																												btnPassaUpdate,
																												GroupLayout.PREFERRED_SIZE,
																												131,
																												GroupLayout.PREFERRED_SIZE))
																												.addGroup(
																														gl_contentPane
																														.createSequentialGroup()
																														.addContainerGap()
																														.addComponent(
																																panel_1,
																																GroupLayout.PREFERRED_SIZE,
																																439,
																																GroupLayout.PREFERRED_SIZE))
																																.addGroup(
																																		gl_contentPane
																																		.createSequentialGroup()
																																		.addContainerGap()
																																		.addComponent(
																																				listScroller_1,
																																				GroupLayout.PREFERRED_SIZE,
																																				565,
																																				GroupLayout.PREFERRED_SIZE)))
																																				.addContainerGap(18, Short.MAX_VALUE)));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(
				Alignment.LEADING).addGroup(
						gl_contentPane
						.createSequentialGroup()
						.addComponent(lblNewLabel)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 129,
								GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_contentPane
										.createParallelGroup(
												Alignment.BASELINE, false)
												.addComponent(btnZoek)
												.addComponent(btnWisGegevens)
												.addComponent(btnPassagierVerwijderen)
												.addComponent(btnPassaUpdate))
												.addPreferredGap(ComponentPlacement.RELATED)
												.addComponent(listScroller_1,
														GroupLayout.PREFERRED_SIZE, 167,
														GroupLayout.PREFERRED_SIZE)
														.addContainerGap(63, Short.MAX_VALUE)));

		JLabel lblE = new JLabel(
				"<html><table style ='background-color:#DAA520 ;'><tr><td width = '70px'>PassagierId</td> <td width = '130px'>Voornaam</td><td width = '130px'>Achternaam</td><td width = '100px'>Vluchtnummer</td></tr></table></html>");
		listScroller_1.setColumnHeaderView(lblE);
		listScroller_1.setViewportView(gevondenPassagiersJList);

		gevondenPassagiersJList.setLayoutOrientation(JList.VERTICAL);
		gevondenPassagiersJList.setVisibleRowCount(-1);
		gevondenPassagiersJList.setSelectedIndex(0);
		gevondenPassagiersJList
		.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		gevondenPassagiersJList.setModel(lstPassagierString);

		contentPane.setLayout(gl_contentPane);

	}

	public void fillModel() {
		// tekstvakken in model ingeven of leeg maken

		if (txtVoornaam.getText() != null) {
			_model.setVoornaam(txtVoornaam.getText());
		} else {
			_model.setVoornaam(null);
		}

		if (txtAchternaam.getText() != null) {
			_model.setFamilienaam(txtAchternaam.getText());
		} else {
			_model.setFamilienaam(null);
		}

		if (cmbVluchtnummer.getSelectedItem() != "1") {
			_model.setVlucht((String) cmbVluchtnummer.getSelectedItem());
		} else {
			_model.setVlucht(null);
		}
	}


	public void resetten () {

		_model.setVoornaam("");
		_model.setFamilienaam("");
		_model.setPassagierId(null);
		_model.setPassagierslijst(null);
		_model.setVlucht(null);

		ZoekPassagierController.clearView(_model);

		// focus op voornaam zetten
		txtPassagierID.grabFocus();



	}



}
